const express = require("express");
const pool = require("../pool.js");

const rRouter = express.Router();
module.exports = rRouter;

// 根据用户id查预约单
rRouter.get("/findById", (req, res, next) => {
  let token = req.header("authorization");
  let uid;
  verify(token, (msg) => (uid = msg.uid));
  var page = req.query.page || 1;
  const pagesize = 10;
  var offset = (page - 1) * pagesize;
  var sql = `select count(rid) as count from reserve where uid=?`;
  pool.query(sql, [uid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    let pagecount = Math.ceil(r[0].count / pagesize);
    var sql = `select * from reserve where uid=? limit ?,?`;
    pool.query(sql, [uid, offset, pagesize], (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, data: r, pagecount });
    });
  });
});
// 新增预约单
rRouter.post("/insert", (req, res, next) => {
  var uname = req.body.uname;
  var docname = req.body.docname;
  var rtime = req.body.rtime;
  var rkind = req.body.rkind;
  var rsymptoms = req.body.rsymptoms;
  var rstate = req.body.rstate;

  var sql = `insert into reserve values(replace(UUID(),'-',''),?,?,?,?,?,?)`;
  pool.query(
    sql,
    [uname, docname, rtime, rkind, rsymptoms, rstate],
    (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, msg: "插入成功" });
    }
  );
});
// 删除预约单
rRouter.delete("/delete", (req, res, next) => {
  var rid = req.body.rid;
  var sql = `delete from reserve where rid=?`;
  pool.query(sql, [rid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.affectedRows != 0) {
      res.send({ code: 200, msg: "删除成功" });
    } else {
      res.send({ code: 201, msg: "删除失败" });
    }
  });
});
// 修改预约单状态
rRouter.put("/updateState", (req, res, next) => {
  var rid = req.body.rid;
  var newstate = req.body.newstate;
  var sql = `update reserve set rstate=? where rid=?`;
  pool.query(sql, [newstate, rid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.affectedRows != 0) {
      res.send({ code: 200, msg: "修改成功" });
    } else {
      res.send({ code: 201, msg: "修改失败" });
    }
  });
});
// 修改整个预约单
rRouter.put("/update", (req, res, next) => {
  var rid = req.body.rid;
  var uname = req.body.uname;
  var docname = req.body.docname;
  var rtime = req.body.rtime;
  var rkind = req.body.rkind;
  var rsymptoms = req.body.rsymptoms;
  var rstate = req.body.rstate;
  if (
    docname == "" ||
    rtime == "" ||
    rkind == "" ||
    rsymptoms == "" ||
    rstate == ""
  ) {
    res.send({ code: 202, msg: "输入不可为空" });
    return;
  }
  var sql = `update reserve set uname=?,docname=?,rtime=?,rkind=?,rsymptoms=?,rstate=? where rid=?`;
  pool.query(
    sql,
    [uname, docname, rtime, rkind, rsymptoms, rstate, rid],
    (err, r) => {
      if (err) {
        next(err);
        return;
      }
      if (r.affectedRows != 0) {
        res.send({ code: 200, msg: "修改成功" });
      } else {
        res.send({ code: 201, msg: "修改失败" });
      }
    }
  );
});
// 查找所有预约单
rRouter.get("/findAll", (req, res, next) => {
  var page = req.query.page || 1;
  const pagesize = 10;
  var offset = (page - 1) * pagesize;
  var sql = `select count(rid) as count from reserve`;
  pool.query(sql, (err, r) => {
    if (err) {
      next(err);
      return;
    }
    let pagecount = Math.ceil(r[0].count / pagesize);
    var sql = `select * from reserve limit ?,?`;
    pool.query(sql, [offset, pagesize], (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, data: r, pagecount });
    });
  });
});
// 根据id查预约单详情
rRouter.get("/find", (req, res, next) => {
  var rid = req.query.rid;
  var sql = `select * from reserve where rid=?`;
  pool.query(sql, [rid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, data: r });
  });
});
